#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow) {
    ui->setupUi(this);
    //
    ui->splitter->setStretchFactor(0, 0);
    ui->splitter->setStretchFactor(1, 1);

    //
    QStringList headers;
    headers << "标志" << "" << "" << "";

    tables.push_back(ui->tblMainDivine);
    tables.push_back(ui->tblFollowDivine);
    for (auto& t : tables) {
        t->setHorizontalHeaderLabels(headers);
        t->setColumnCount(4);
        t->setRowCount(10);
        //
        for (int i = 0; i < t->rowCount(); ++i) {
            t->setRowHeight(i, 10);
        }
        //
        t->setColumnWidth(0, 20);
        t->setColumnWidth(1, 60);
        t->setColumnWidth(2, 60);
        t->setColumnWidth(3, 60);
    }
}

MainWindow::~MainWindow() {
    delete ui;
}


void MainWindow::on_tblMainDivine_cellClicked(int row, int column) {
    if (column == 0) {
        return;
    }

    // Toggle the selection
    // bool ret = !ui->workTable->item(row, column)->data(Qt::DisplayRole).toBool();
    int val = ui->workTable->item(row, column)->data(Qt::DisplayRole).toInt();
    int state = 0;
    switch (val) {
    case hft::kSstTrade:
        state = hft::kSstPause; break;
    case hft::kSstPause:
        state = hft::kSstStop; break;
    case hft::kSstStop:
        state = hft::kSstTrade; break;
    default:
        // 不存在的状态
        return;
    }


    QItemSelectionModel* selection = ui->workTable->selectionModel();
    // Check if we have a selection, and the row we clicked on is within the selection
    bool is_selected = false;
    foreach (QModelIndex index, selection->selectedRows()) {
        if (index.row() == row) {
            is_selected = true;
            break;
        }
    }

    if (is_selected) {
        foreach (QModelIndex index, selection->selectedRows()) {
            ui->workTable->item(index.row(), column)->setData(Qt::DisplayRole, state);
        }
    } else {
        ui->workTable->item(row, column)->setData(Qt::DisplayRole, state);
    }

    updateWorkColors();
}
